package com.youi.web;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.youi.exception.BusinessException;
import com.youi.manage.HttpRequestManage;
import com.youi.manage.HttpSessionManage;
import com.youi.common.DelStatus;
import com.youi.common.SessionKey;
import com.youi.service.GroupleaderService;
import com.youi.service.LeaderprofitService;
import com.youi.service.LeaderprofitdetailService;
import com.youi.entity.Groupleader;
import com.youi.entity.Leaderprofit;
import com.youi.entity.Leaderprofitdetail;
import com.youi.entity.User;
import com.youi.common.Common;
import com.youi.util.CommonInfoUtil;
import com.youi.util.DateUtil;
import com.youi.util.JedisUtils;
import com.youi.util.JsonUtil;
import com.youi.util.ObjectUtil;
import com.youi.util.Page;

/**
* 团长佣金信息控制层
* @author Administrator
*/
@Controller
public class LeaderprofitController  extends HttpRequestManage{

		@Autowired
		private LeaderprofitService leaderprofitService;
		@Autowired
		private GroupleaderService groupleaderService;
		@Autowired
		private LeaderprofitdetailService leaderprofitdetailService;
	
		/**
		 * M-22
		 * 团长佣金信息
		 * @param modelMap
		 * @param request
		 * @return
		 * @throws BusinessException
		 */
		@ResponseBody
		@RequestMapping("userGetInfo/leader/getLeaderprofit")
		public String getLeaderprofitById(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			User user=(User) HttpSessionManage.getAttribute(request, SessionKey.USER);			
			Map<String, Object> prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);
			prams.put("leaderId", user.getLeaderId());
			Leaderprofit leaderprofit=leaderprofitService.getByParam(prams);
			if (leaderprofit==null) {
				leaderprofitService.saveOrUpdate(new Leaderprofit(user.getLeaderId()));
			}
			modelMap.put("frozen", leaderprofit.getFrozen());
			modelMap.put("balance", leaderprofit.getBalance());
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",modelMap);//jsonObject	json对象
		}

		/**
		 * M-23
		 * 团长佣金详情
		 * @param modelMap
		 * @param request
		 * @return
		 * @throws BusinessException
		 * @throws ParseException 
		 */
		@ResponseBody
		@RequestMapping("userGetInfo/leader/getLeaderprofitDetail")
		public String getLeaderprofitDetail(ModelMap modelMap,HttpServletRequest request) throws BusinessException, ParseException{
			User user=(User) HttpSessionManage.getAttribute(request, SessionKey.USER);
			Map<String, Object> prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);
			prams.put("leaderId", user.getLeaderId());
			Leaderprofit leaderprofit=leaderprofitService.getByParam(prams);
			if (leaderprofit==null) {
				leaderprofitService.saveOrUpdate(new Leaderprofit(user.getLeaderId()));
			}
			Double todayAmt=0.00;
			Double monthAmt=0.00;
			prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);
			prams.put("leaderId", user.getLeaderId());
			prams.put(Common.TYPE, Arrays.asList(0,2));
			Map<String, Object> startTime=new HashMap<String, Object>();
			startTime.put(Common.GMTCREATED, DateUtil.simpdfyMd.format(new Date())+" 00:00:00");
			Map<String, Object> endTime=new HashMap<String, Object>();
			endTime.put(Common.GMTCREATED, DateUtil.simpdfyMd.format(new Date())+" 23:59:59");			
			List<Leaderprofitdetail> leaderprofitdetailList=leaderprofitdetailService.getByParamsAll(prams, null, null, startTime, endTime);
			if (leaderprofitdetailList!=null&&leaderprofitdetailList.size()>0) {
				for (Leaderprofitdetail leaderprofitdetail : leaderprofitdetailList) {
					todayAmt+=leaderprofitdetail.getAmount();
				}
			}
			prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);
			prams.put("leaderId", user.getLeaderId());
			prams.put(Common.TYPE, Arrays.asList(0,2));
			startTime=new HashMap<String, Object>();
			startTime.put(Common.GMTCREATED, DateUtil.simpdfyMd.format(DateUtil.getDateMonthFirstDay(new Date()))+" 00:00:00");
			endTime=new HashMap<String, Object>();
			endTime.put(Common.GMTCREATED, DateUtil.simpdfyMd.format(DateUtil.getDateMonthLastDay(new Date()))+" 23:59:59");
			leaderprofitdetailList=leaderprofitdetailService.getByParamsAll(prams, null, null, startTime, endTime);
			if (leaderprofitdetailList!=null&&leaderprofitdetailList.size()>0) {
				for (Leaderprofitdetail leaderprofitdetail : leaderprofitdetailList) {
					monthAmt+=leaderprofitdetail.getAmount();
				}
			}
			DecimalFormat todayAmtFormat=new DecimalFormat("0.00");
			modelMap.put("todayAmt", todayAmtFormat.format(todayAmt));//本日佣金
			DecimalFormat monthAmtFormat=new DecimalFormat("0.00");
			modelMap.put("monthAmt", monthAmtFormat.format(monthAmt));//本月佣金			
			modelMap.put("totalAmount", leaderprofit.getTotalAmount());//累计佣金
			modelMap.put("frozen", leaderprofit.getFrozen());
			modelMap.put("balance", leaderprofit.getBalance());
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",modelMap);
		}
}
